package com.pocketsweet.ui.uilib.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.pocketsweet.R;
import com.pocketsweet.model.MLArticle;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.Topic;
import com.pocketsweet.ui.TopicArticle;

/**
 * 
 * @author Kam
 * 
 */
public class LikeRecommendAdapter extends BaseListAdapter<MLArticle> {
	public LikeRecommendAdapter(Context ctx) {
		super(ctx);
	}

	public LikeRecommendAdapter(Context ctx, List<MLArticle> datas) {
		super(ctx, datas);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_like_recommend, parent,
					false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.refreshView(datas.get(position), position);
		return convertView;
	}

	private class ViewHolder implements View.OnClickListener {
		ImageView imgAvatar;
		TextView tvTitle;
		MLArticle item;
		private View parent;

		private ViewHolder(View parent) {
			this.parent = parent;
			initView(parent);
		}

		public void initView(View parent) {
			imgAvatar = (ImageView) parent.findViewById(R.id.imgAvatar);
			tvTitle = (TextView) parent.findViewById(R.id.tvTitle);
		}

		public void refreshView(MLArticle item, int i) {
			this.item = item;
			String avatar = item.getTitleImgUrl();
			if (avatar != null && !avatar.equals("")) {
				UserService.displaySquare(avatar, imgAvatar);
			} else {
				imgAvatar.setImageResource(R.drawable.default_user_avatar);
			}
			// 设置标题
			tvTitle.setText(item.getTitle());
			parent.setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			if (v.equals(parent)) {
				//  查看文章
				Intent intent = new Intent(ctx, TopicArticle.class);
				intent.putExtra("articleId", item.getObjectId());
				intent.putExtra("title", item.getTitle());
				intent.putExtra("intro", item.getIntro());
				intent.putExtra("likeCount", String.valueOf(item.getLikeCount()));
				intent.putExtra("shareCount", String.valueOf(item.getShareCount()));
				intent.putExtra("commentCount", String.valueOf(item.getCommentCount()));
				ctx.startActivity(intent);
			}
		}
	}
}
